package com.itic.develop.study.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itic.appbase.applications.sys.common.service.OutCommonService;
import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.constants.IticConst;
import com.itic.appbase.framework.common.constants.ResponseCode;
import com.itic.appbase.framework.common.controller.BaseController;
import com.itic.appbase.framework.exception.definition.ParameterErrorException;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.appbase.framework.utils.ValueHelper;
import com.itic.develop.study.persistence.model.Study;
import com.itic.develop.study.persistence.model.StudyExample;
import com.itic.develop.study.persistence.model.StudyExample.Criteria;
import com.itic.develop.study.service.StudyService;


@Controller
@RequestMapping(value="/develop/study")
public class StudyController
     extends BaseController {
    @Autowired
    private StudyService studyService;
    @Autowired
   	protected OutCommonService outCommonService;

   	////////////////////////////////////////////////////// 常量区////////////////////////////////////////////////////////////////
   	private static final String MODEL_NAME_CHINA = "培训管理";// 中文名
   	private static final String MODEL_NAME＿ENGLISH = "study";// 英文名
   	private static final String PAGE_COMMON_PATH = "/develop/study/study";// 通用路径
   	private static final String COMMON_URL = "/develop/study/";// 通用路径
   	private static final String TYPE = "T_DM_T_DEVELOP_RULE";// 代码表名称

   	// 导出常量
   	private static final String TEMP = "bzmc";// 备注名称
   	private static final String DATALIST = "dataList";// 数据常量
   	// 返回属性黑名单
       private static String[] ignoreProperties = new String[] { };
   	////////////////////////////////////////////////////// 页面跳转区////////////////////////////////////////////////////////////

   	/*
   	 * 主页面
   	 */
   	@RequestMapping(value = "/init")
   	public String init(Model model) {
   		model.addAttribute("name_china", MODEL_NAME_CHINA);
   		model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
   		model.addAttribute("common_url", COMMON_URL);
   		return PAGE_COMMON_PATH + "Main";
   	}

   	/*
   	 * 添加页面
   	 */
   	@RequestMapping(value = "/initAdd")
   	public String initAdd(Model model) {
   		model.addAttribute("name_china", MODEL_NAME_CHINA);
   		model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
   		model.addAttribute("common_url", COMMON_URL);
   		return PAGE_COMMON_PATH + "Add";
   	}

   	/*
   	 * 编辑页面
   	 */
   	@RequestMapping(value = "/initEdit")
   	public String initEdit(String id, Model model) {
   		if (StringHelper.isNotBlank(id)) {
   			Study Study = studyService.get(id);
   			model.addAttribute("editData", iticJsonMapper.toJson(Study));
   			model.addAttribute("name_china", MODEL_NAME_CHINA);
   			model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
   			model.addAttribute("common_url", COMMON_URL);
   		} else {
   			throw new ParameterErrorException();
   		}
   		return PAGE_COMMON_PATH + "Edit";
   	}

   	/*
   	 * 查看页面
   	 */
   	@RequestMapping(value = "/initView")
   	public String initView(String id, Model model) {
   		if (StringHelper.isNotBlank(id)) {

   			Study Study = studyService.get(id);
   			Study.setType(outCommonService.getNameByCode(TYPE, Study.getType()));
       		
   			model.addAttribute("viewEdit", iticJsonMapper.toJson(Study));
   			model.addAttribute("name_china", MODEL_NAME_CHINA);
   			model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
   			model.addAttribute("common_url", COMMON_URL);
   		} else {
   			throw new ParameterErrorException();
   		}

   		return PAGE_COMMON_PATH + "View";
   	}

   	////////////////////////////////////////////////////// 业务代码区////////////////////////////////////////////////////////////

   	/**
   	 * 添加模板管理数据
   	 * 
   	 * @param android
   	 * @return
   	 */
   	@RequestMapping(value = "/add")
   	@ResponseBody
   	public Map<String, String> add(Study temp) {

   		Map<String, String> resultMap = new HashMap<String, String>();
   		String result = ResponseCode.SERVER_ERROR.getCode();
   		String unescapeXml = StringEscapeUtils.unescapeXml(temp.getContent());
        temp.setContent(unescapeXml);
        
   		result = studyService.add(temp);

   		resultMap.put(IticConst.RESULT, result);
   		return resultMap;
   	}

   	/**
   	 * <p>
   	 * Title:queryCondition
   	 * </p>
   	 * <p>
   	 * Description:封装查询条件
   	 * </p>
   	 * 
   	 * @param jzdw
   	 * @param jzdwExample
   	 * @return
   	 */
   	private StudyExample queryCondition(HttpServletRequest request, Study Study) {

   		// 参数处理
   		StudyExample example = new StudyExample();
   		// 按创建时间倒序
   		example.setOrderByClause("UPDATE_TIME DESC");
   		// 按排列号顺序
   		//example.setOrderByClause("reorder asc");

   	    Criteria criteria = example.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE);

   		// 过滤组织机构
   		// User user = findUserSession(request);
   		// criteria.andOrgCodeEqualTo(user.getOrganizationCode());

   		// 查询条件
   		if (null != Study) {
   			// 模糊查询
   			if (StringHelper.isNotBlank(Study.getTaget())) {
   				criteria.andTagetLike("%" + Study.getTaget() + "%");
   			}
   			if( StringHelper.isNotBlank(Study.getType())){
   				criteria.andTypeEqualTo(Study.getType());
   			}
   		}
   		return example;
   	}

   	/**
   	 * 
   	 * @param goldType
   	 * @param request
   	 * @return
   	 */
   	@RequestMapping(value = "/list")
   	@ResponseBody
   	public Map<String, Object> list(Study Study, HttpServletRequest request) {
   		// 声明返回对象
   		Map<String, Object> resultMap = new HashMap<String, Object>();

   		StudyExample example = this.queryCondition(request, Study);
   		// 封装分页参数
   		example = this.handlePageQuery(request, example);

   		// 获取信息总条数
   		int total = studyService.count(example);

   		// 获取数据
   		List<Study> list = studyService.list(example);

   		// 结果集
   		List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
   		if (list.size() > 0) {
   			for( Study deve : list){
   				deve.setType(outCommonService.getNameByCode(TYPE, deve.getType()));
   				if( deve.getContent().length()>40 ){
					deve.setContent(deve.getContent().substring(0,40));
				}
           	}
   			// 过滤黑名单字段
   			resultList = ValueHelper.tranList2MapList(list, ignoreProperties);
   		}

   		// 数据返回处理
   		resultMap.put(IticConst.TOTAL_RECORDS, total);
   		resultMap.put(IticConst.DISPLAY_RECORDS, total);
   		resultMap.put(IticConst.RESULT, ResponseCode.SUCCESS.getCode());
   		resultMap.put(IticConst.DATA, resultList);
   		return resultMap;

   	}

   	/**
   	 * 编辑保存
   	 * 
   	 * @param templet
   	 * @return
   	 */
   	@RequestMapping(value = "/edit")
   	@ResponseBody
   	public Map<String, String> edit(String delFileIds, Study temp) {
   		Map<String, String> resultMap = new HashMap<String, String>();
   		String result = ResponseCode.SERVER_ERROR.getCode();
   		String unescapeXml = StringEscapeUtils.unescapeXml(temp.getContent());
        temp.setContent(unescapeXml);
   		result = studyService.edit(temp);

   		resultMap.put(IticConst.RESULT, result);
   		return resultMap;
   	}

   	/**
   	 * 删除
   	 * 
   	 */
   	@RequestMapping(value = "/del")
   	@ResponseBody
   	public Map<String, String> del(String id) {
   		Map<String, String> resultMap = new HashMap<String, String>();
   		String result = ResponseCode.SERVER_ERROR.getCode();

   		// 删除一个
   		if (StringHelper.isNotBlank(id)) {
   			Study Study = new Study();
   			Study.setId(id);
   			result = studyService.delete(Study);
   		}

   		resultMap.put(IticConst.RESULT, result);
   		return resultMap;
   	}

}